package com.seguriapp.alerts.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.geocode.StatusCodes;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.seguriapp.alerts.gwt.client.activity.AlertDetailsActivity;
import com.seguriapp.alerts.gwt.client.activity.AlertDetailsActivity.Display;
import com.seguriapp.common.gwt.client.UtilClientValidations;
import com.seguriapp.entities.Alert;

public class AlertDetailsPage extends Composite implements Display {

  private static AlertDetailsPageUiBinder uiBinder = GWT.create(AlertDetailsPageUiBinder.class);

  interface AlertDetailsPageUiBinder extends UiBinder<Widget, AlertDetailsPage> {
  }

  private AlertDetailsActivity presenter;

  private MapWidget map;
  private Geocoder geocoder;

  @UiField
  protected HTMLPanel mapPanel;
  @UiField
  protected HTMLPanel errorMessage;
  @UiField
  protected HTMLPanel detailsContent;
  @UiField
  protected Label nameslbl;
  @UiField
  protected Label dnilbl;
  @UiField
  protected Label datelbl;
  @UiField
  protected Label placelbl;
  @UiField
  protected Label phonelbl;

  public AlertDetailsPage() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  public void setPresenter(final AlertDetailsActivity presenter) {
    this.presenter = presenter;
  }

  public void setData(final Alert alert) {
    errorMessage.setVisible(false);
    detailsContent.setVisible(true);
    final LatLng latLng = LatLng.newInstance(alert.getLatitude(), alert.getLongitude());
    map = new MapWidget(latLng, 13);
    map.setSize("500px", "300px");
    final Marker marker = new Marker(latLng);
    map.addOverlay(marker);
    map.setScrollWheelZoomEnabled(true);
    map.addControl(new SmallMapControl());
    getLocationData(latLng);
    nameslbl.setText(alert.getNames().concat(" ").concat(alert.getLastNames()));
    dnilbl.setText(alert.getDni());
    datelbl.setText(UtilClientValidations.DATE_VIEW_FORMAT.format(alert.getDate()));
    phonelbl.setText(alert.getPhone());
    mapPanel.clear();
    mapPanel.add(map);
  }

  public void setNoDataMessage() {
    errorMessage.setVisible(true);
    detailsContent.setVisible(false);
  }

  @UiHandler("backToListLink")
  protected void onBackToListLinkClick(final ClickEvent event) {
    presenter.goBackToList();
  }

  private void getLocationData(final LatLng latLng) {
    geocoder = new Geocoder();
    geocoder.getLocations(latLng, new LocationCallback() {

      public void onFailure(int statusCode) {
        Window.alert("Failed to geocode position " + latLng + ". Status: " + statusCode + " " + StatusCodes.getName(statusCode));
      }

      public void onSuccess(final JsArray<Placemark> locations) {
        if (locations.length() > 0) {
          Placemark location = locations.get(0);
          StringBuilder value = new StringBuilder();
          if (location.getAddress() != null) {
            value.append(location.getAddress());
          } else {
            if (location.getCity() != null) {
              value.append(location.getCity());
            }
            if (location.getAdministrativeArea() != null) {
              value.append(location.getAdministrativeArea() + ", ");
            }
            if (location.getCountry() != null) {
              value.append(location.getCountry());
            }
          }
          placelbl.setText(value.toString());
        }
      }
    });

  }
}
